package greedy.onClass.class2.exer01;
import java.util.Scanner;

class Interval {
    int l; // 左
    int r; // 右

    Interval(int l, int r) {
        this.l = l;
        this.r = r;
    }
}

public class Main {
    static void sort(Interval[] a, int n) {
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                if (a[j].l > a[j + 1].l) {
                    Interval temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                } else if (a[j].l == a[j + 1].l) {
                    if (a[j].r > a[j + 1].r) {
                        Interval temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Interval[] a = new Interval[n];
        int rm = 0; // 右的最大范围
        int lm = 1; // 左
        int maxindex = -1;
        int flag = 0; // 区间是否变动标志位

        for (int i = 0; i < n; i++) {
            a[i] = new Interval(scanner.nextInt(), scanner.nextInt());
        }

        sort(a, n);

        for (int i = 0; i < n; i++) {
            if (a[i].l == 1 && a[i].r > rm) { // 左区间为1时的最大范围
                rm = a[i].r;
                maxindex = i;
            }
        }

        System.out.println(lm + " " + rm);

        int i = maxindex; // 从最大范围起始位置开始
        while (i < n) {
            flag = 0;
            for (int j = i + 1; j < n; j++) {
                if (a[i].r >= a[j].l || a[i].r + 1 == a[j].l) {
                    // [1,7][6,10]或[1,7][8,10]覆盖的都是a[i].l,a[j].r
                    if (a[j].r > rm) {
                        rm = a[j].r;
                        lm = a[j].l;
                        flag = 1;
                        maxindex = j - 1;
                    }
                }
            }
            if (flag == 1) {
                i = maxindex;
                System.out.println(lm + " " + rm);
            } else {
                i++;
            }
        }

        scanner.close();
    }
}
